package com.example.tests;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.views.RegisterActivity;

public class RegisterTests extends ActivityInstrumentationTestCase2 <RegisterActivity> {
	
	private RegisterActivity mActivity;
	private EditText nameField;
	private TextView errorText;
	private Button createAccount;
	
	public RegisterTests() {
		super(RegisterActivity.class);
	}
	
	public void setUp() {
		setActivityInitialTouchMode(false);
		
		mActivity = getActivity();
		
		nameField = (EditText) mActivity.findViewById(
				com.example.R.id.text_username);
		
		errorText = (TextView) mActivity.findViewById(
				com.example.R.id.login_error_text);
		
		createAccount = (Button) mActivity.findViewById(
				com.example.R.id.button_create_account);
	}
	
	public void testTaken() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("u s e r n a m e");
					assertEquals("Username Taken", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testTooShort() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("a a a a");
					assertEquals("Invalid Username", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testTooLong() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("a a a a a a a a a a a a a a a a a");
					assertEquals("Invalid Username", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testBadFirstChar() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("1 a a a a");
					assertEquals("Invalid Username", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testBadChar() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("a a A * a");
					assertEquals("Invalid Username", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testShort() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("a a a a a");
					assertEquals("Invalid Password", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void testLong() {
		try {
			runTestOnUiThread(new Runnable() {

				@Override
				public void run() {
					testName("a a a a a a a a a a a a a a a a");
					assertEquals("Invalid Password", errorText.getText());
				}
			}); 
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	private void testName(String name) {
		nameField.requestFocus();
		sendKeys(name);
		
		createAccount.performClick();
	}
	
}